package com.example.gkcircle.activity;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.camnter.easyslidingtabs.widget.EasySlidingTabs;
import com.example.gkcircle.BaseAppCompatActivity;
import com.example.gkcircle.R;
import com.example.gkcircle.adapter.TabsFragmentAdapter;
import com.example.gkcircle.fragment.lost.search.SearchFoundFragment;
import com.example.gkcircle.fragment.lost.search.SearchLostFragment;
import com.example.gkcircle.utils.CommonUtil;
import com.example.gkcircle.utils.MySVProgressHUD;
import com.example.gkcircle.utils.ToastFactory;
import com.example.gkcircle.widget.EditTextWithDelete;

import java.util.LinkedList;
import java.util.List;

/**
 * 搜索界面
 * Created by Jehu on 16/3/20.
 */
public class SearchLostActivity extends BaseAppCompatActivity implements View.OnClickListener{

    private ImageButton mbackBtn;
    private EditTextWithDelete mSearchEdit;
    private TextView mSearch;

    private EasySlidingTabs easySlidingTabs;
    private ViewPager easyVP;
    private TabsFragmentAdapter adapter;
    List<Fragment> fragments;
    public static final String[] titles = {"失物", "招领" };

    private int currentPage=0;
    private final int maxDataCount=10;

    @Override
    protected void initView() {
        setContentView(R.layout.search_lost_activity);
        this.mbackBtn=(ImageButton)this.findViewById(R.id.search_back);
        this.mSearchEdit=(EditTextWithDelete)this.findViewById(R.id.search_edit);
        this.mSearch=(TextView)this.findViewById(R.id.search_tv);
        this.easySlidingTabs = (EasySlidingTabs) this.findViewById(R.id.search_easy_sliding_tabs);
        this.easyVP = (ViewPager) this.findViewById(R.id.search_easy_vp);
    }

    @Override
    protected void initListener() {
        mbackBtn.setOnClickListener(this);
        mSearch.setOnClickListener(this);
    }

    @Override
    protected void setData() {
        this.fragments = new LinkedList<>();
        SearchLostFragment searchLostFragment = SearchLostFragment.getInstance();
        SearchFoundFragment searchFoundFragment = SearchFoundFragment.getInstance();
        this.fragments.add(searchLostFragment);
        this.fragments.add(searchFoundFragment);
        this.adapter = new TabsFragmentAdapter(this.getSupportFragmentManager(), titles, this.fragments);
        this.easyVP.setAdapter(this.adapter);
        this.easySlidingTabs.setViewPager(this.easyVP);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_back:
                finish();
                break;
            //搜索
            case R.id.search_tv:
                int flag=easyVP.getCurrentItem();
                String keyword=mSearchEdit.getText().toString().trim();
                if(keyword.isEmpty()){
                    ToastFactory.getToast(this,getResources().getString(R.string.search_input_keyword)).show();
                    return;
                }
                MySVProgressHUD.getInstance(this).showWithStatus(getResources().getString(R.string.searching));
                CommonUtil.hideSoftInputView(this);
                search(flag, keyword);
                break;
        }
    }

    //搜索
    private void search(int flag,final String keyword){
        switch (flag){
            //失物
            case 0:
                AVQuery<AVObject> queryLost=new  AVQuery("Lost");
                queryLost.whereContains("content",keyword);
                queryLost.setLimit(SearchLostFragment.maxDataCount);
                queryLost.setSkip(SearchLostFragment.maxDataCount*SearchLostFragment.currentPage);
                queryLost.orderByDescending("createdAt");
                queryLost.include("user");
                queryLost.findInBackground(new FindCallback<AVObject>() {
                    @Override
                    public void done(List<AVObject> list, AVException e) {
                        //回调给searchLostFragment处理数据
                        SearchLostFragment.iSetSearchLostListener.complete(list,e,keyword);
                        MySVProgressHUD.getInstance(SearchLostActivity.this).dismiss();
                    }
                });
                break;
            //招领
            case 1:
                AVQuery<AVObject> queryFound=new  AVQuery("Found");
                queryFound.whereContains("content",keyword);
                queryFound.setLimit(SearchFoundFragment.maxDataCount);
                queryFound.setSkip(SearchFoundFragment.maxDataCount*SearchFoundFragment.currentPage);
                queryFound.orderByDescending("createdAt");
                queryFound.include("user");
                queryFound.findInBackground(new FindCallback<AVObject>() {
                    @Override
                    public void done(List<AVObject> list, AVException e) {
                        //回调给searchFoundFragment处理数据
                        SearchFoundFragment.iSetSearchFoundListener.complete(list,e,keyword);
                        MySVProgressHUD.getInstance(SearchLostActivity.this).dismiss();
                    }
                });
                break;
            default:
                break;
        }
    }



}
